// share/statistics/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CurrentPage: 1,
    loadingMoreHidden: 1,
    staticsType: 0,
    recordList: [],
    checkList:[],
    report: {},
    sendTotal: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.staticsType){
      if (options.staticsType == 2) {
        this.getCheckList(options.creatUid);
      }
      if (options.staticsType == 1) {
        this.getPackageRecardList(options.creatUid);
      }
      this.setData({
        staticsType: options.staticsType,
        creatUid: options.creatUid
      })
    }
    
  },

  // 切换Tab
  changeStatictype: function(e){
    let staticsType = e.currentTarget.dataset.statictype;
    this.setData({
      staticsType: staticsType,
      CurrentPage: 1
    })
    if (staticsType == 2) {
      this.getCheckList(this.data.creatUid);
    }
    if (staticsType == 1) {
      this.getPackageRecardList(this.data.creatUid);
    }
    
  },

  // 股东礼包发放记录
  getPackageRecardList: function (creatUid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetPackageRecardList?creatUid=' + creatUid +'&shareUid=' + app.globalData.userInfo.Uid + '&page=' + that.data.CurrentPage + '&gradeId=0',
      success: function (res) {
        if (res.data.Code == 0) {
          let recordList = res.data.Data.List;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.recordList
            recordList = preGoods.concat(recordList);
          }
          for (let i = 0; i < recordList.length; i++) {
            recordList[i].CreateTime = recordList[i].CreateTime.substr(0, 10);
          }
          that.setData({
            recordList: recordList,
            sendTotal: res.data.Data.PageModel.TotalCount,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          }) 
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  // 股东礼包核销记录
  getCheckList: function (creatUid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetCheckList?shareUid=' + app.globalData.userInfo.Uid + '&createUid=' + creatUid + '&page=' + that.data.CurrentPage + '&gradeId=0',
      success: function (res) {
        if (res.data.Code == 0) {
          let checkList = res.data.Data.List;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.checkList
            checkList = preGoods.concat(checkList);
          }
          for (let i = 0; i < checkList.length; i++) {
            checkList[i].CreateTime = checkList[i].CreateTime.substr(0, 10);
          }
          that.setData({
            checkList: checkList,
            report: res.data.Data.Report,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  // 加载更多
  loadMore: function (e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      if(that.data.staticsType==1){
        that.getPackageRecardList(that.data.creatUid);//发放记录
      }
      if (that.data.staticsType == 2){
        that.getCheckList(that.data.creatUid);
      }
    } else {
      return
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

})